package com.gxa.pingan.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.gxa.pingan.common.JsonResult;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.InsuranceManagerMapper;
import com.gxa.pingan.pojo.InsuranceManager;
import com.gxa.pingan.service.InsuranceManagerService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.xmlbeans.impl.jam.mutable.MPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/insuranceManager")
@Api(value = "保险的控制器", tags = "保险的接口的api")
public class InsuranceManagerController {
    private final static Integer SIZE = 10;

    @Autowired
    InsuranceManagerService insuranceManagerService;

    @Autowired
    InsuranceManagerMapper insuranceManagerMapper;

    @PostMapping("/findByPage")
    @ApiOperation("这是保险分页查询的方法")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "请输入页码", type = "Int", required = true),
            @ApiImplicitParam(name = "size", value = "请输入大小", type = "Int", required = true)
    })
    public Map findByPage(@RequestParam Integer page, @RequestParam Integer size) {

        List<Map> all = insuranceManagerService.findAll();
        List<Map> allByPage = insuranceManagerService.findAllByPage(page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("count", all.size());
        jsonResult.setData(allByPage);
        return jsonResult.getDataMap();
    }

    @PostMapping("/findNameOrPhoneByPage")
    @ApiOperation("这是保险根据电话或名字模糊查询的方法")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "请输入页码", type = "Int", required = true),
            @ApiImplicitParam(name = "page", value = "每页大小", type = "int", required = true),
            @ApiImplicitParam(name = "nameOrPhone", value = "模糊查询姓名或电话号码", type = "string", required = false),

    })

    public Map findNameOrPhoneByPage(String nameOrPhone, Integer page, Integer size) {

        List<Map> list = insuranceManagerMapper.findNameOrPhone(nameOrPhone);
        List<Map> byPage = insuranceManagerService.findNameOrPhoneByPage(nameOrPhone, page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.put("count", list.size());
        jsonResult.setData(byPage);
        return jsonResult.getDataMap();
    }

    @PostMapping("/add")
    @ApiOperation("添加保险")
    public Map add(@RequestBody InsuranceManager insuranceManager) throws JsonProcessingException, BusinessException {

        insuranceManagerService.add(insuranceManager);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/delete")
    @ApiOperation("删除保险的操作")
    public Map delete(@RequestParam Long id) {

        insuranceManagerService.delete(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/update")
    @ApiOperation("修改保险的操作")
    public Map update(@RequestBody InsuranceManager insuranceManager) throws BusinessException {
        insuranceManagerService.update(insuranceManager.getId(), insuranceManager);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/findByIdToUpdate")
    @ApiOperation("根据id获取数据")
    public Map findByIdToUpdate(Integer id) {
        InsuranceManager byIdToUpDate = insuranceManagerService.findByIdToUpDate(id);
        System.out.println("byIdToUpDate: " + byIdToUpDate);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(byIdToUpDate);
        System.out.println(jsonResult.getDataMap());
        return jsonResult.getDataMap();
    }
}
